package com.xiechuang.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: IO助手
 * @Author: xiechuang
 * @Date: 2023/1/11
 */
public class IOHelper {

    private static final Logger logger = LoggerFactory.getLogger(IOHelper.class);
    private static final String CLASS_NAME = IOHelper.class.getName();

    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    public static void closeQuietly(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

    public static long copyLarge(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

    public static int copy(InputStream input, OutputStream output) throws IOException {
        long count = copyLarge(input, output);
        if (count > Integer.MAX_VALUE) {
            return -1;
        }
        return (int) count;
    }

    public static final byte[] read(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = input.read(buff, 0, 100)) > 0) {
            output.write(buff, 0, rc);
        }
        input.close();
        output.close();
        return output.toByteArray();
    }

    /**
     * 读取流
     *
     * @param stream
     * @param charset
     * @return
     */
    public static String read(InputStream stream, String... charset) {
        StringBuffer strBuffer = new StringBuffer();
        try {
            List<String> lines = readLines(stream, ValidHelper.isEmpty(charset) ? "UTF-8" : charset[0]);
            for (int i = 0; i < lines.size(); i++) {
                strBuffer.append(lines.get(i));
                if (i < lines.size()) {
                    strBuffer.append("\n");
                }
            }
        } catch (Exception ex) {
            logger.error(CLASS_NAME + "：", ex);
        }
        return strBuffer.toString();
    }

    public static List<String> readLines(InputStream input, String encoding) throws IOException {
        if (encoding == null) {
            return readLines(input);
        } else {
            InputStreamReader reader = new InputStreamReader(input, encoding);
            return readLines(reader);
        }
    }

    public static List<String> readLines(InputStream input) throws IOException {
        InputStreamReader reader = new InputStreamReader(input);
        return readLines(reader);
    }

    public static List<String> readLines(Reader input) throws IOException {
        BufferedReader reader = new BufferedReader(input);
        List<String> list = new ArrayList<String>();
        String line = reader.readLine();
        while (line != null) {
            list.add(line);
            line = reader.readLine();
        }
        return list;
    }

    public static byte[] toBytes(String value, String encoding) {
        try {
            return value.getBytes(encoding);
        } catch (Exception ex) {
            logger.error(CLASS_NAME + "：", ex);
        }
        return new byte[]{};
    }

}
